<?php

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;
use App\Lib\Utils;

/**
 * Class Index. 
 * @package App\HttpController
 */
class FormatSubData extends Controller
{

    public function index()
    {

    }

    /**
     * 格式化发送给设备的数据
     * @param  [type] $data      [设备信息]
     * @param  [type] $Act       [对设备进行的操作]
     * @param  [type] $setParams [设置的参数]
     * @return [type]            [description]
     */
    public function format($data, $Act, $setParams = array())
    {
        // 获取对设备操作的名称和动作
        $deviceINI = \Yaconf::get("deviceAct");

        $ActName = $deviceINI['name'];
        $Action = $deviceINI['act'];

        $HTTPVersion = intval(preg_replace( '/[^0-9 ]/i', '', $data['http_version']));
        $DeviceUUID = $data['device_uuid'];
        $UserName = $data['deviceuser'];
        $PassWord = $data['devicepwd'];
        $DeviceID = $data['device_id'];
        $strSession = $data['session'];
        $TimeStamp = time();

        $Sign = strtolower(md5($DeviceUUID.':'.$UserName.':'.$PassWord.':'.$TimeStamp));
        $strActName = $ActName[$Act] ?? '';
        $strAction = $Action[$Act] ?? '';

        // Post JSON数据
        $Pdata = array(
            "Name" => $strActName,
            "UUID" => $DeviceUUID,
            "Session" => $strSession,
            "TimeStamp" => $TimeStamp,
            "Sign" => $Sign
        );

        if ($Act == 1) {
            // 获取整个名单列表
            
        } elseif($Act == 2) {
            //添加单个名单列表
            if(isset($data['starttime']) && ($data['starttime'] > 1550000000)){
                $strStartTime = date('Y-m-d H:i:s', $data['starttime']);
            }else{
                $strStartTime = date('Y-m-d H:i:s', time());
            }

            if(isset($data['endtime']) && ($data['endtime'] > 1550000000)){
                $strEndTime = date('Y-m-d H:i:s', $data['endtime']);
            }elseif(isset($data['lifetime']) && ($data['lifetime'] > 1550000000)){
                $strEndTime = date('Y-m-d H:i:s', $data['lifetime']);
            }else{
                $strEndTime = date('Y-m-d H:i:s', strtotime('+5 year'));
            }
            //访客名单
            if($data['mode'] == 4){
                $data['mode'] = 2;
            }
            $subData = [
                "Action" => $strAction,
                "PersonType" => intval($data['mode']),
                "PersonInfo" => [
                    "PersonId" => strval($data['faceid']),
                    "PersonName" => $data['name'],
                    "Sex" => $data['sex']?intval($data['sex']):2,
                    "IDCard" => $data['idcard'],
                    "Nation" => $data['nation'],
                    "Birthday" => $data['birthday'],
                    "Phone" => $data['mobile'],
                    "LimitTime" => intval($data['limittime']),
                    "StartTime" => $strStartTime,
                    "EndTime" => $strEndTime,
                    "Label" => $data['remark'],
                    "ImgPath" => $data['imgpath'],
                    "PersonExtension" => [
                        "PersonParam1" => intval($data['idcard'])
                    ],
                    "PersonPhoto" => "IMG/9j/4AAQSK/img"
                ]

            ];

            $Pdata['Data'] = $subData;

            $imgData = Utils::readImage(EASYSWOOLE_ROOT.'/webroot'.$data['imgpath'], 0);
        }elseif($Act == 3){
            //修改单个名单列表
            //添加单个名单列表
            if(isset($data['starttime']) && ($data['starttime'] > 1550000000)){
                $strStartTime = date('Y-m-d H:i:s', $data['starttime']);
            }else{
                $strStartTime = date('Y-m-d H:i:s', time());
            }

            if(isset($data['endtime']) && ($data['endtime'] > 1550000000)){
                $strEndTime = date('Y-m-d H:i:s', $data['endtime']);
            }elseif(isset($data['lifetime']) && ($data['lifetime'] > 1550000000)){
                $strEndTime = date('Y-m-d H:i:s', $data['lifetime']);
            }else{
                $strEndTime = date('Y-m-d H:i:s', strtotime('+5 year'));
            }
            //访客名单
            if($data['mode'] == 4){
                $data['mode'] = 2;
            }
            $subData = [
                "Action" => $strAction,
                "PersonType" => intval($data['mode']),
                "ChangePhoto" => 1,
                "PersonInfo" => [
                    "PersonId" => strval($data['faceid']),
                    "PersonName" => $data['name'],
                    "Sex" => $data['sex']?intval($data['sex']):2,
                    "IDCard" => $data['idcard'],
                    "Nation" => $data['nation'],
                    "Birthday" => $data['birthday'],
                    "Phone" => $data['mobile'],
                    "LimitTime" => intval($data['limittime']),
                    "StartTime" => $strStartTime,
                    "EndTime" => $strEndTime,
                    "Label" => $data['remark'],
                    "ImgPath" => $data['imgpath'],
                    "PersonExtension" => [
                        "PersonParam1" => intval($data['idcard'])
                    ],
                    "PersonPhoto" => "IMG/9j/4AAQSK/img"
                ]

            ];

            $Pdata['Data'] = $subData;

            $imgData = Utils::readImage(EASYSWOOLE_ROOT.'/webroot'.$data['imgpath'], 0);
        }elseif($Act == 4){
            //删除名单列表，0-所有名单，1-黑名单，2-白名单，3-VIP名单
            $subData = [
                "Action" => $strAction,
                "PersonType" => intval($setParams['personType'])
            ];

            $Pdata['Data'] = $subData;
        }elseif($Act == 5){
            //访客名单
            if($data['mode'] == 4){
                $data['mode'] = 2;
            }
            //删除单个名单列表
            $subData = [
                "Action" => $strAction,
                "PersonType" => intval($data['mode']),
                "PersonId" => strval($data['faceid'])
            ];

            $Pdata['Data'] = $subData;
        }elseif($Act == 6){
            //删除单个人员
            
        }elseif($Act == 7){
            //批量添加人员信息
            
        }elseif($Act == 8){
            //批量删除人员信息
            
        } elseif($Act == 9) {

            // IO输出控制 默认3秒
            $subData = array(
                "ContinueSeconds" => 3
            );
            $Pdata['Data'] = $subData;

        }elseif($Act == 10){
            //获取设备http设置参数
            
        }elseif($Act == 11){
            //设置设备http设置参数

        } elseif($Act == 12) { // 时间同步

            $subData = array(
                "TimeMode" => 1,
                "LocalTime" => date("Y-m-d H:i:s",time())
            );
            $Pdata['Data'] = $subData;
            
        }elseif($Act == 13){
            //导入一张人脸获取返回特征值
            
        }elseif($Act == 14){
            //人脸名单库搜索
            
        }elseif($Act == 15){
            //人脸识别参数获取
            
        }elseif($Act == 16){
            //人脸识别参数设置
            
        }elseif($Act == 17){
            //人脸识别报警参数获取
            
        }elseif($Act == 18){
            //人脸识别报警参数设置
            
        }elseif($Act == 19){
            //网络参数获取
            
        }elseif($Act == 20){
            //网络参数设置
            
        }elseif($Act == 21){
            //重启设备
            
        } elseif ($Act == 22) { // 激活设备

            $baseINI = \Yaconf::get("basebt");
            $middleUrl = rtrim($baseINI['reqDomain'], '/').':'.$baseINI['middlePort'];

            // 激活设备，让设备主动注册上来
            $subData = [
                "DomainName" => rtrim($baseINI['reqDomain'], '/'),
                "Port" =>   intval($baseINI['port']),
                "RegisterPath" =>   $baseINI['deviceReg'],
                "HeartbeatPath" =>  $baseINI['deviceOnline'],
                "CaptureInfoPath" =>    $baseINI['deviceComp'],
                "DeviceSN" =>   $data['device_remark'],
                "DeviceAdmin" =>    $data['deviceuser'],
                "DevicePassword" => $data['devicepwd'],
                "MiddleWareAddress" => $middleUrl,
            ];

            $Pdata['Data'] = $subData;
        }

        $Pdata = json_encode($Pdata, JSON_UNESCAPED_SLASHES);

        if (isset($imgData) && (($strAction == 'addPerson') || ($strAction == 'editPerson'))) {
            $Pdata = str_replace('IMG/9j/4AAQSK/img', $imgData, $Pdata);
        }

        return $Pdata;
    }

}
